package com.yugao.fintech.draper.multicache.support;

import com.yugao.fintech.draper.cache.provider.DistributedCache;
import com.yugao.fintech.draper.cache.provider.DistributedCacheProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
public class RedisCacheProvider implements DistributedCacheProvider {

    private final RedisTemplate redisTemplate;

    private Map<String, DistributedCache> cacheManagerMap = new HashMap<>();

    @Override
    public <T> DistributedCache<T> getOrCreate(String cacheName) {
        return cacheManagerMap.computeIfAbsent(cacheName, name -> {
            BoundHashOperations hashOps = redisTemplate.boundHashOps(cacheName);
            return new RedisCache<T>(hashOps);
        });
    }
}
